package edu.etime.xsj.goods.consumer.controllers;

import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.loadbalancer.IRule;
import edu.etime.xsj.goods.consumer.common.Message;
import edu.etime.xsj.goods.consumer.pojo.GoodsType;
import edu.etime.xsj.goods.consumer.pojo.GoodsTypeList;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.Service;
import java.util.List;

/**
 * 消费者接口
 */
@RestController
@RequestMapping("/consumer/goodstype")
@DefaultProperties(defaultFallback = "selectGoodsTypeListByBalanceFallback")
public class GoodsTypeController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/all")
    public List<GoodsType> selectAllGoodsType(){
        GoodsTypeList goodsTypeList = restTemplate.getForObject("http://127.0.0.1:8080/service/goodstype/all", GoodsTypeList.class);
        return goodsTypeList.getGoodstypelist();
    }

    /**
     * 通过服务实例调用
     * @return
     */
    @RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public GoodsTypeList selectGoodsTypeList(){
        List<ServiceInstance> instanceList = discoveryClient.getInstances("goodstypeservice");
        for(ServiceInstance instance : instanceList){
            System.out.println(instance.getUri());
        }
        //获取一个服务的实例
        ServiceInstance instance = instanceList.get(0);
        //System.out.println(instance.getUri().toString());
        GoodsTypeList goodsTypeList = restTemplate.getForObject(instance.getUri()+"/service/goodstype/all", GoodsTypeList.class);
        return goodsTypeList;
    }

    /**
     * 通过负载均衡来进行服务调用
     * @return
     */
    @RequestMapping(value = "/balance", produces = MediaType.APPLICATION_JSON_VALUE)
    @HystrixCommand(commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3500")
    })
    public Message selectGoodsTypeListByBalance(){
        GoodsTypeList goodsTypeList = restTemplate.getForObject("http://goodstypeservice/service/goodstype/all", GoodsTypeList.class);
        Message<GoodsTypeList> message = new Message<>();
        message.setCode(0);
        message.setData(goodsTypeList);
        return message;
    }

    @RequestMapping(value = "/balance1", produces = MediaType.APPLICATION_JSON_VALUE)
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="10"), //10次
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000"),//休眠10秒
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="60")
    })
    public Message selectGoodsTypeListByBalance1(Integer id){
        if(id % 2 == 0){
            throw new RuntimeException();
        }
        GoodsTypeList goodsTypeList = restTemplate.getForObject("http://goodstypeservice/service/goodstype/all", GoodsTypeList.class);
        Message<GoodsTypeList> message = new Message<>();
        message.setCode(0);
        message.setData(goodsTypeList);
        return message;
    }

    public Message selectGoodsTypeListByBalanceFallback(){
        Message message = new Message();
        message.setCode(1);
        message.setMsg("请求失败");
        return message;
    }
}
